import type { RouteRecordRaw } from 'vue-router'
import type { IBreadcrumb, IMenuItem, IMenuList } from '@/types'

export function mapMenusToRoutes(menuData: IMenuList) {
  const localRoutes: RouteRecordRaw[] = []

  // 自动读取路由配置
  const files: Record<string, any> = import.meta.glob('@/router/main/**/*.ts', {
    eager: true
  })
  for (const key in files) {
    const module = files[key].default
    localRoutes.push(module)
  }
  const routes: RouteRecordRaw[] = []
  // 动态添加路由
  for (const item of menuData.menuList) {
    for (const subItem of item.subMenus) {
      const route = localRoutes.find((route) => route.path === subItem.route)
      if (route) routes.push(route)
    }
  }
  return routes
}

/**
 * 获取当前路由的面包屑
 * params: path: 当前路由地址
 * params: useMenus: 菜单数据
 */

export function mapPathBreadcrumbs(path: string, useMenus: IMenuItem[]) {
  const breadcrumbs: IBreadcrumb[] = []
  useMenus.forEach((menu) => {
    menu.subMenus.forEach((s: IMenuItem) => {
      if (s.route === path) {
        breadcrumbs.push({ name: menu.title, path: menu.route }, { name: s.title, path: s.route })
      }
    })
  })
  return breadcrumbs
}
